package com.lyd.sweet.mybatis.mapper;

import org.apache.ibatis.mapping.MappedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

/**
 * 类的用途
 *
 * @author 木木
 **/
public class SelectProvider extends MapperTemplate {

    private Logger logger = LoggerFactory.getLogger(SelectProvider.class);

    public SelectProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String selectOneByField(MappedStatement ms) {

        Class<?> entityClass = this.getEntityClass(ms);
        this.setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.selectAllColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, this.tableName(entityClass)));
        sql.append(" <where>");
        sql.append("<if test=\"field != null\" >");
        sql.append(" and ${field}=#{value}");
        sql.append("</if>");
        sql.append("</where>");
        return sql.toString();
    }

    public String selectOneByTwoField(MappedStatement ms) {

        Class<?> entityClass = this.getEntityClass(ms);
        this.setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.selectAllColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, this.tableName(entityClass)));
        sql.append(" <where>");
        sql.append("<if test=\"oneField != null\" >");
        sql.append(" and ${oneField}=#{oneValue}");
        sql.append("</if>");
        sql.append("<if test=\"twoField != null\" >");
        sql.append(" and ${twoField}=#{twoValue}");
        sql.append("</if>");
        sql.append("</where>");
        return sql.toString();
    }


}
